<?php
App::uses('AppController', 'Controller');
/**
 * Subjects Controller
 *
 * @property Subject $Subject
 */
class SubjectsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	
	public function index() {
	    $this->GetOption('subject_fields'); //get setting of subject_fields
		$this->Subject->recursive = 0;
		$this->set('subjects', $this->paginate());
		 
	}

	public function viewsubjects($branch_id = null) {
		$this->GetOption('subject_fields'); //get setting of subject_fields
		if (!$branch_id) {
			throw new NotFoundException(__('Invalid data'));
		}
		     //Controller::loadModel('Branchsubject');
		     
  $this->paginate=array("Subject"=>
   array(
 "conditions"=> array(
 "Subject.id in (select subject_id from branchsubjects where branch_id=$branch_id )"
   )

 ));
		$this->set("viewsubjects",$this->paginate());
	}
	
	
	
public function viewshareds($stage_id = null) {
		$this->GetOption('subject_fields'); //get setting of subject_fields
		if (!$stage_id) {
			throw new NotFoundException(__('Invalid data'));
		}
		     //Controller::loadModel('Branchsubject');
		     
  $this->paginate=array("Subject"=>
   array(
 "conditions"=> array(
 "Subject.id in (select subject_id from branchsubjects where branch_id in (select id from branches where stage_id= $stage_id)  GROUP BY subject_id having count(*) > 1 )"
   )

 ));
		$this->set("viewshareds",$this->paginate());
	}
	
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
	    $this->GetOption('subject_fields'); //get setting of subject_fields
		$this->Subject->id = $id;
		if (!$this->Subject->exists()) {
			throw new NotFoundException(__('Invalid data'));
		}
		$this->set('subject', $this->Subject->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add($branch_id=null) {
		$this->GetOption('subject_fields'); //get setting of subject_fields
		if ($this->request->is('post')) {
		
		 if(isset($this->request->data["Subject"]["user_id"])){
		        $useId =AuthComponent::user('id');		
		        //$useId ="1";				
				$this->request->data["Subject"]["user_id"]=$useId;
				}
				
			$this->Subject->create();
			if ($this->Subject->save($this->request->data)) {
				$id=$this->Subject->id;
				$this->savelog($id);
				
	            Controller::loadModel('Branchsubject');
	             
				$subject_branch["Branchsubject"]= array(
					'subject_id' => $id,
					'branch_id' => $branch_id,
					'user_id' => $useId
					);
					$this->Branchsubject->save($subject_branch);
					
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'viewsubjects/'.$branch_id));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		}
		$terms = $this->Subject->Term->find('list');
		$users = $this->Subject->User->find('list');
/*		$students = $this->Subject->Student->find('list');*/
		
	  if(!$branch_id){
		
	    Controller::loadModel('Stage'); 
		$this->Stage->recursive = 1;
		//$stages=$this->Stage->find("list",array("order"=>"level"));
		$stages_branche=$this->Stage->find("all",array("order"=>"level"));
		$this->set("stages_branche",$stages_branche);
		$this->render("stages");
	  }
		else{
		Controller::loadModel('Branch'); 
		$this->Branch->recursive = 1;
		$branche=$this->Branch->read(null,$branch_id);
		$this->set(compact('branche')); 
		
		}
		$this->set(compact('terms', 'users', 'students'));
	}
public function shared($stage_id=null) {
		$this->GetOption('subject_fields'); //get setting of subject_fields
		if ($this->request->is('post')) {
		
		 if(isset($this->request->data["Subject"]["user_id"])){
		        $useId =AuthComponent::user('id');		
		        //$useId ="1";				
				$this->request->data["Subject"]["user_id"]=$useId;
				}
				
			$this->Subject->create();
			if ($this->Subject->save($this->request->data)) {
				$id=$this->Subject->id;
				$this->savelog($id);
				
	            Controller::loadModel('Branchsubject');
	        $this->Subject->query("INSERT INTO branchsubjects (branchsubjects.subject_id,branchsubjects.branch_id) 
(SELECT '".$id."' AS subject_id , branches.id AS branch_id FROM branches WHERE stage_id=$stage_id)");
	          
//				$subject_branch["Branchsubject"]= array(
//					'subject_id' => $id,
//					'branch_id' => $branch_id,
//					'user_id' => $useId
//					);
//					$this->Branchsubject->save($subject_branch);
					
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		}
		$terms = $this->Subject->Term->find('list');
		$users = $this->Subject->User->find('list');
/*		$students = $this->Subject->Student->find('list');*/
		
	  if(!$stage_id){
		
	    Controller::loadModel('Stage'); 
		$this->Stage->recursive = 1;
		//$stages=$this->Stage->find("list",array("order"=>"level"));
		$stages_branche=$this->Stage->find("all",array("order"=>"level"));
		$this->set("stages_branche",$stages_branche);
		$this->render("shared");
	  }
		else{
		Controller::loadModel('Stage'); 
		$this->Stage->recursive = 0;
		$stage=$this->Stage->read(null,$stage_id);
		$this->set(compact('stage')); 
		
		}
		$this->set(compact('terms', 'users', 'students'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		 $this->GetOption('subject_fields'); //get setting of subject_fields
		$this->Subject->id = $id;
		if (!$this->Subject->exists()) {
			throw new NotFoundException(__('Invalid subject'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
		
		 if(isset($this->request->data["Subject"]["user_id"])){
		      $useId =AuthComponent::user('id');		
		      		
				$this->request->data["Subject"]["user_id"]=$useId;
				}
				
			if ($this->Subject->save($this->request->data)) {
			      $this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		} else {
			$this->request->data = $this->Subject->read(null, $id);
		}
		$terms = $this->Subject->Term->find('list');
		//$users = $this->Subject->User->find('list');
		//$students = $this->Subject->Student->find('list');
		$this->set(compact('terms', 'users', 'students'));
	   // $this->render("add");
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Subject->id = $id;
		if (!$this->Subject->exists()) {
			throw new NotFoundException(__('Invalid subject'));
		}
		if ($this->Subject->delete()) {
		       $this->savelog($id);
				
			$this->Session->setFlash(__('Data has been deleted'), 'default',array("class"=>"mws-form-message success"));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Data was not deleted'), array('action' => 'index'), array('action' => 'index'));
		$this->redirect(array('action' => 'index'));
	}
	
}
